#include "floating_menu_widget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QScreen>
#include <QGuiApplication>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
#include <QCursor>
#include <QDebug>
#include <QFile>
#include <QPixmap>
#include <QSettings>
#include <QtCore/QProcess>
#include <QtCore/QDir>
#ifdef Q_OS_WIN
#include <windows.h>
#include <QWindow>
#endif

FloatingMenuWidget::FloatingMenuWidget(QWidget *parent)
    : QWidget(parent)
    , m_roundMenu(nullptr)
    , m_trayIcon(nullptr)
    , m_trayMenu(nullptr)
    , m_isDragging(false)
    , m_canDrag(false)
    , m_longPressTimer(nullptr)
#ifdef Q_OS_WIN
    , m_draggingWithWinAPI(false)
    , m_hwnd(nullptr)
#endif
    , m_opacityAnimation(nullptr)
    , m_shadowEffect(nullptr)
{
    setupUI();
    setupSystemTray();
    // setupMenuItems(); // 移除重复调用，菜单项将在 loadSettings() 中设置
}

FloatingMenuWidget::~FloatingMenuWidget()
{
    if (m_trayIcon) {
        m_trayIcon->hide();
    }
}

void FloatingMenuWidget::setupUI()
{    // 设置窗口属性 - 重新启用透明背景，但使用更稳定的配置
    setWindowFlags(Qt::FramelessWindowHint | 
                   Qt::WindowStaysOnTopHint | 
                   Qt::Tool);
    
    // 重新启用透明背景，使用稳定的配置
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_NoSystemBackground, true);  // 禁用系统背景
    setAttribute(Qt::WA_OpaquePaintEvent, false);   // 允许透明绘制
    
    // 移除样式表，使用完全透明背景    // 使用现代化 Fluent Design 样式表 - 协调按钮配色
    setStyleSheet(
        "FloatingMenuWidget {"
        "    background: qlineargradient(x1:0, y1:0, x2:1, y2:1, "
        "        stop:0 rgba(255, 255, 255, 250), "
        "        stop:0.5 rgba(248, 250, 255, 245), "
        "        stop:1 rgba(245, 248, 255, 240));"
        "    border-radius: 150px;"
        "    border: 2px solid rgba(64, 158, 255, 120);"  // 与按钮颜色协调的边框
        "    box-shadow: 0 8px 32px rgba(64, 158, 255, 25);"  // 柔和阴影
        "}"
    );
    
    // 设置固定大小
    setFixedSize(300, 300);
    
    // 创建圆形菜单
    m_roundMenu = new RoundMenuWidget(this);
    m_roundMenu->setFixedSize(300, 300);  // 让菜单填满整个窗口    // 配置菜单样式 - 现代化美观风格
    MenuStyleConfig style = m_roundMenu->getMenuStyle();
    style.showBackground = false;        // 不显示背景
    style.useRoundMenuItems = true;      // 使用圆形菜单项
    style.showCenterButton = true;       // 显示中心按钮
    style.useScatteredLayout = false;    // 默认使用圆形布局（非分散布局）
    style.enableRotationAnimation = true;
    style.animationDuration = 400;       // 更流畅的动画时长
    
    // 优化大小比例，更协调的视觉效果
    style.outerRadiusScale = 0.80;                    // 稍大的外圆半径
    style.innerRadiusScale = 0.35;                    // 稍小的内圆半径，更突出中心
    style.centerButtonScale = BUTTON_SIZE_SCALE * 1.1; // 稍大的中心按钮
    style.menuItemBgScale = BUTTON_SIZE_SCALE;        // 菜单项大小
      // 现代化渐变蓝色配色方案 - 优化按钮颜色
    style.centerButtonColor = QColor(64, 158, 255);         // 鲜亮科技蓝
    style.centerButtonHoverColor = QColor(40, 140, 245);    // 深科技蓝悬停    style.centerButtonExpandColor = QColor(255, 107, 129);  // 温暖粉红色展开状态
      m_roundMenu->setMenuStyle(style);
    
    // 加载保存的布局设置
    QSettings settings("FloatingMenu", "Settings");
    int savedLayout = settings.value("layout", SettingsDialog::CircularLayout).toInt();
    applyLayoutSettings(static_cast<SettingsDialog::LayoutType>(savedLayout));
    
    // 设置菜单图标
    m_roundMenu->setExpandIcon(":/icons/float/menu.svg");
    m_roundMenu->setCollapseIcon(":/icons/float/close.svg");
      // 布局 - 不使用边距，让菜单完全填充窗口
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);  // 去除边距
    layout->addWidget(m_roundMenu, 0, Qt::AlignCenter);
    
    // 移除阴影效果，避免分层窗口问题
    // m_shadowEffect = new QGraphicsDropShadowEffect(this);
    // m_shadowEffect->setBlurRadius(20);
    // m_shadowEffect->setColor(QColor(0, 0, 0, 160));
    // m_shadowEffect->setOffset(0, 5);
    // setGraphicsEffect(m_shadowEffect);
    
    // 初始化透明度动画
    m_opacityAnimation = new QPropertyAnimation(this, "windowOpacity", this);
    m_opacityAnimation->setDuration(200);
    
    // 初始化长按定时器
    m_longPressTimer = new QTimer(this);
    m_longPressTimer->setSingleShot(true);
    m_longPressTimer->setInterval(LONG_PRESS_DURATION);
    connect(m_longPressTimer, &QTimer::timeout, this, &FloatingMenuWidget::onLongPressTimer);
      // 连接信号
    connect(m_roundMenu, &RoundMenuWidget::menuItemClicked, 
            this, &FloatingMenuWidget::onMenuItemClicked);
    connect(m_roundMenu, &RoundMenuWidget::menuToggled, 
            this, &FloatingMenuWidget::onMenuToggled);
      // 默认位置 - 屏幕右下角
    QScreen *screen = QGuiApplication::primaryScreen();
    if (screen) {
        QRect screenGeometry = screen->availableGeometry();
        move(screenGeometry.width() - width() - 50, 
             screenGeometry.height() - height() - 50);
    }
    
    // 加载保存的设置
    loadSettings();
      // 设置初始窗口遮罩
    // updateWindowMask();  // 移除，使用透明背景替代
#ifdef Q_OS_WIN    // 获取 Windows 窗口句柄用于原生拖动
    if (windowHandle()) {
        m_hwnd = (HWND)windowHandle()->winId();
        // qDebug() << "获取 Windows 句柄:" << m_hwnd;
    }
#endif
}

void FloatingMenuWidget::setupSystemTray()
{
    // 检查系统是否支持系统托盘
    if (!QSystemTrayIcon::isSystemTrayAvailable()) {
        QMessageBox::critical(nullptr, "系统托盘", 
                             "系统不支持系统托盘功能");
        return;
    }
    
    // 创建系统托盘图标
    m_trayIcon = new QSystemTrayIcon(this);
      // 设置图标 - 使用flashlight图标
    QIcon trayIconImage;
    
    // 首先尝试从资源文件加载
    if (QFile::exists(":/icons/float/flashlight.svg")) {
        trayIconImage = QIcon(":/icons/float/flashlight.svg");
    } else {
        // 如果资源文件不存在，创建一个简单的图标
        QPixmap pixmap(16, 16);
        pixmap.fill(QColor(100, 150, 200));
        trayIconImage = QIcon(pixmap);
    }
    
    m_trayIcon->setIcon(trayIconImage);
    m_trayIcon->setToolTip("浮动圆形菜单");
    
    // 创建托盘菜单
    m_trayMenu = new QMenu(this);
    
    QAction *showAction = m_trayMenu->addAction("显示菜单");
    QAction *hideAction = m_trayMenu->addAction("隐藏菜单");
    m_trayMenu->addSeparator();
    QAction *quitAction = m_trayMenu->addAction("退出");
    
    // 连接托盘菜单动作
    connect(showAction, &QAction::triggered, this, &FloatingMenuWidget::showWindow);
    connect(hideAction, &QAction::triggered, this, &FloatingMenuWidget::hideWindow);
    connect(quitAction, &QAction::triggered, this, &FloatingMenuWidget::quitApplication);
    
    m_trayIcon->setContextMenu(m_trayMenu);
    
    // 连接托盘图标信号
    connect(m_trayIcon, &QSystemTrayIcon::activated, 
            this, &FloatingMenuWidget::onTrayIconActivated);
    
    // 显示托盘图标
    m_trayIcon->show();
}

void FloatingMenuWidget::setupMenuItems()
{
    // 确保清除现有菜单项，防止重复
    m_roundMenu->clearMenuItems();
    
    // 创建图标加载辅助函数
    auto loadIcon = [](const QString &iconPath, int size = 32) -> QPixmap {
        QPixmap pixmap;
        if (QFile::exists(iconPath)) {
            pixmap.load(iconPath);
            if (!pixmap.isNull()) {
                pixmap = pixmap.scaled(size, size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            }
        }
        return pixmap;
    };    // 添加功能菜单项 - 使用现代 Fluent Design 配色
    QPixmap qqIcon = loadIcon(":/icons/float/qq.svg");
    QPixmap qqHoverIcon = loadIcon(":/icons/float/qq-fill.svg");
    QPixmap qqPressedIcon = loadIcon(":/icons/float/qq-fill.svg");
    m_roundMenu->addMenuItem("QQ", "打开腾讯QQ", qqIcon, qqHoverIcon, qqPressedIcon, QColor(255, 255, 255, 220)); // 纯净白色
    
    QPixmap wechatIcon = loadIcon(":/icons/float/wechat.svg");
    QPixmap wechatHoverIcon = loadIcon(":/icons/float/wechat-fill.svg");
    QPixmap wechatPressedIcon = loadIcon(":/icons/float/wechat-fill.svg");
    m_roundMenu->addMenuItem("微信", "打开微信", wechatIcon, wechatHoverIcon, wechatPressedIcon, QColor(255, 255, 255, 220)); // 纯净白色
    
    QPixmap settingsIcon = loadIcon(":/icons/float/settings.svg");
    QPixmap settingsHoverIcon = loadIcon(":/icons/float/settings-fill.svg");
    QPixmap settingsPressedIcon = loadIcon(":/icons/float/settings-fill.svg");
    m_roundMenu->addMenuItem("设置", "系统设置", settingsIcon, settingsHoverIcon, settingsPressedIcon, QColor(255, 255, 255, 220)); // 纯净白色
    
    QPixmap terminalIcon = loadIcon(":/icons/float/terminal.svg");
    QPixmap terminalHoverIcon = loadIcon(":/icons/float/terminal-fill.svg");
    QPixmap terminalPressedIcon = loadIcon(":/icons/float/terminal-fill.svg");
    m_roundMenu->addMenuItem("终端", "打开终端", terminalIcon, terminalHoverIcon, terminalPressedIcon, QColor(255, 255, 255, 220)); // 纯净白色
    
    m_roundMenu->addMenuItem("文件管理", "打开文件管理器", 
                            ":/icons/float/folder.svg", 
                            ":/icons/float/folder-fill.svg", 
                            QColor(255, 255, 255, 220)); // 纯净白色
    
    m_roundMenu->addMenuItem("浏览器", "打开默认浏览器", 
                            ":/icons/float/chrome.svg", 
                            ":/icons/float/chrome-fill.svg", 
                            QColor(255, 255, 255, 220));  // 纯净白色
    
    m_roundMenu->addMenuItem("截图", "截图工具", 
                            ":/icons/float/screenshot.svg", 
                            ":/icons/float/screenshot-fill.svg", 
                            QColor(255, 255, 255, 220));        // 纯净白色
      m_roundMenu->addMenuItem("关于", "关于此应用", 
                            ":/icons/float/about.svg", 
                            ":/icons/float/about-fill.svg", 
                            QColor(255, 255, 255, 220));      // 纯净白色
}

void FloatingMenuWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {        // 检查是否按下了 Shift 键
        if (event->modifiers() & Qt::ShiftModifier) {
            // Shift + 左键 → 启动原生拖动
            startNativeDrag();
            event->accept(); // 接受事件，不传递给子控件
            return;
        } else {
            // 普通左键点击也尝试拖动（简化用户操作）
            startNativeDrag();
            event->accept();
            return;
        }
    }
    
    // 其他情况传递事件给子控件处理菜单展开/收起
    QWidget::mousePressEvent(event);
}

void FloatingMenuWidget::mouseMoveEvent(QMouseEvent *event)
{
    // 使用原生拖动，不需要在这里处理
    QWidget::mouseMoveEvent(event);
}

void FloatingMenuWidget::mouseReleaseEvent(QMouseEvent *event)
{
    // 使用原生拖动，不需要在这里处理
    QWidget::mouseReleaseEvent(event);
}

void FloatingMenuWidget::enterEvent(QEnterEvent *event)
{
    Q_UNUSED(event)
    // 重新启用温和的透明度动画
    if (m_opacityAnimation) {
        m_opacityAnimation->setStartValue(windowOpacity());
        m_opacityAnimation->setEndValue(1.0);
        m_opacityAnimation->start();
    }
}

void FloatingMenuWidget::leaveEvent(QEvent *event)
{
    Q_UNUSED(event)
    // 重新启用温和的透明度动画
    if (m_opacityAnimation && !m_roundMenu->isExpanded()) {
        m_opacityAnimation->setStartValue(windowOpacity());
        m_opacityAnimation->setEndValue(0.9);
        m_opacityAnimation->start();
    }
}

void FloatingMenuWidget::showEvent(QShowEvent *event)
{
    Q_UNUSED(event)
    // 重新启用透明度，但使用更保守的值
    setWindowOpacity(0.9);
}

void FloatingMenuWidget::closeEvent(QCloseEvent *event)
{
    // 关闭窗口时隐藏到系统托盘而不是退出
    if (m_trayIcon && m_trayIcon->isVisible()) {
        hide();
        event->ignore();
        
        // 首次隐藏时显示提示
        static bool firstHide = true;
        if (firstHide) {
            m_trayIcon->showMessage("浮动菜单", 
                                   "应用已最小化到系统托盘",
                                   QSystemTrayIcon::Information, 2000);
            firstHide = false;
        }
    } else {
        event->accept();
    }
}

void FloatingMenuWidget::onMenuItemClicked(int index)
{    // 根据索引执行相应功能
    switch (index) {
    case 0: // QQ
        // 更新QQ路径：D:\Program Files\Tencent\QQNT\QQ.exe
        if (!QProcess::startDetached("QQ.exe")) {
            if (!QProcess::startDetached("D:\\Program Files\\Tencent\\QQNT\\QQ.exe")) {
                if (!QProcess::startDetached("C:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQ.exe")) {
                    QProcess::startDetached("C:\\Program Files\\Tencent\\QQ\\Bin\\QQ.exe");
                }
            }
        }
        break;
    case 1: // 微信
        // 更新微信路径：D:\Program Files\Tencent\WeChat\WeChat.exe
        if (!QProcess::startDetached("WeChat.exe")) {
            if (!QProcess::startDetached("D:\\Program Files\\Tencent\\WeChat\\WeChat.exe")) {
                if (!QProcess::startDetached("C:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe")) {
                    QProcess::startDetached("C:\\Program Files\\Tencent\\WeChat\\WeChat.exe");
                }
            }
        }
        break;    case 2: // 设置
        openSettingsDialog();
        break;
    case 3: // 终端
        // 尝试启动Windows Terminal或cmd
        if (!QProcess::startDetached("wt.exe")) {
            QProcess::startDetached("cmd.exe");
        }
        break;    case 4: // 文件管理
        // 打开指定目录：D:\Download\EdgeDownload\win-x64
        QDesktopServices::openUrl(QUrl::fromLocalFile("D:/Download/EdgeDownload/win-x64"));
        break;
    case 5: // 浏览器
        QDesktopServices::openUrl(QUrl("https://www.google.com"));
        break;
    case 6: // 截图
        QProcess::startDetached("SnippingTool.exe");
        break;
    case 7: // 关于
        QMessageBox::about(this, "关于",
                          "浮动圆形菜单 v1.0\n\n"
                          "功能特性:\n"
                          "• 无标题栏浮动窗口\n"
                          "• 点击展开圆形菜单\n"
                          "• 长按拖动移动位置\n"
                          "• 系统托盘集成\n"
                          "• 半透明视觉效果\n"
                          "• 快速启动QQ、微信等应用");
        break;
    default:
        break;
    }
    
    // 执行功能后收起菜单
    m_roundMenu->collapseMenu();
}

void FloatingMenuWidget::onMenuToggled(bool expanded)
{
    // 恢复菜单切换时的优雅透明度动画
    if (m_opacityAnimation) {
        if (expanded) {
            m_opacityAnimation->setStartValue(windowOpacity());
            m_opacityAnimation->setEndValue(1.0);
            m_opacityAnimation->start();
        } else {
            m_opacityAnimation->setStartValue(windowOpacity());
            m_opacityAnimation->setEndValue(0.9);
            m_opacityAnimation->start();
        }
    }
    // 更新窗口遮罩以匹配菜单状态
    // updateWindowMask();  // 移除，使用透明背景替代
}

void FloatingMenuWidget::onTrayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
    case QSystemTrayIcon::DoubleClick:
        showWindow();
        break;
    case QSystemTrayIcon::Trigger:
        if (isVisible()) {
            hideWindow();
        } else {
            showWindow();
        }
        break;
    default:
        break;
    }
}

void FloatingMenuWidget::onLongPressTimer()
{
    // 现在使用 Shift + 左键拖动，不再需要长按检测
    // 保留此方法以避免编译错误
}

void FloatingMenuWidget::showWindow()
{
    show();
    raise();
    activateWindow();
}

void FloatingMenuWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    // 使用改进的透明绘制方式，避免UpdateLayeredWindowIndirect错误
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    
    // 绘制完全透明的背景
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.fillRect(rect(), Qt::transparent);
    
    // 如果需要，可以绘制一个非常淡的调试边框
    if (m_isDragging) {
        painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
        painter.setPen(QPen(QColor(255, 255, 255, 50), 1));
        painter.setBrush(Qt::NoBrush);
        painter.drawEllipse(rect().adjusted(5, 5, -5, -5));
    }
}

void FloatingMenuWidget::hideWindow()
{
    hide();
}

void FloatingMenuWidget::quitApplication()
{
    QApplication::quit();
}

void FloatingMenuWidget::startLongPressTimer()
{
    m_longPressTimer->start();
}

void FloatingMenuWidget::stopLongPressTimer()
{
    m_longPressTimer->stop();
}

void FloatingMenuWidget::enableDragMode()
{
    setCursor(Qt::SizeAllCursor);
    
    // 可以添加视觉反馈，比如改变透明度
    m_opacityAnimation->setStartValue(windowOpacity());
    m_opacityAnimation->setEndValue(0.5);
    m_opacityAnimation->start();
}

void FloatingMenuWidget::disableDragMode()
{
    setCursor(Qt::ArrowCursor);
    
    m_opacityAnimation->setStartValue(windowOpacity());
    m_opacityAnimation->setEndValue(0.7);
    m_opacityAnimation->start();
}

void FloatingMenuWidget::updateWindowMask()
{
    // 移除窗口遮罩，使用透明背景替代
    // 窗口遮罩在拖动时可能导致 UpdateLayeredWindowIndirect 错误
    clearMask();
}

void FloatingMenuWidget::startNativeDrag()
{
#ifdef Q_OS_WIN    // 使用 Windows API 进行原生窗口拖动
    
    // 确保窗口句柄存在
    if (!windowHandle()) {
        return;
    }
    
    HWND hwnd = (HWND)windowHandle()->winId();
    if (!hwnd) {
        return;
    }
    
    // 释放鼠标捕获
    ReleaseCapture();
    
    // 发送原生拖动消息
    LRESULT result = SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
    Q_UNUSED(result); // 避免编译警告
    
#else
    // 非Windows平台的fallback实现
#endif
}

#ifdef Q_OS_WIN
bool FloatingMenuWidget::nativeEvent(const QByteArray &eventType, void *message, qintptr *result)
{
    if (eventType == "windows_generic_MSG") {
        MSG *msg = static_cast<MSG*>(message);
        
        // 处理关键的窗口消息
        switch (msg->message) {
        case WM_LBUTTONDOWN:
            // 在这里也可以尝试启动拖动
            if (GetAsyncKeyState(VK_SHIFT) & 0x8000) {
                ReleaseCapture();
                SendMessage(msg->hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
                *result = 0;
                return true;
            }
            break;
        }
    }
    
    return QWidget::nativeEvent(eventType, message, result);
}
#endif

void FloatingMenuWidget::openSettingsDialog()
{
    SettingsDialog *dialog = new SettingsDialog(this);
    
    // 设置当前布局类型
    MenuStyleConfig currentStyle = m_roundMenu->getMenuStyle();
    SettingsDialog::LayoutType currentLayout = currentStyle.useScatteredLayout ? 
        SettingsDialog::ScatteredLayout : SettingsDialog::CircularLayout;
    dialog->setCurrentLayout(currentLayout);
    
    // 连接信号
    connect(dialog, &SettingsDialog::layoutChanged, 
            this, &FloatingMenuWidget::onLayoutChanged);
    
    // 显示对话框
    dialog->exec();
    
    // 清理
    dialog->deleteLater();
}

void FloatingMenuWidget::onLayoutChanged(SettingsDialog::LayoutType layout)
{
    applyLayoutSettings(layout);
}

void FloatingMenuWidget::applyLayoutSettings(SettingsDialog::LayoutType layout)
{
    MenuStyleConfig style = m_roundMenu->getMenuStyle();
    
    // 根据选择的布局类型更新样式
    if (layout == SettingsDialog::ScatteredLayout) {
        style.useScatteredLayout = true;
        style.enableRotationAnimation = false; // 分散布局通常不需要旋转动画
        style.animationDuration = 200;         // 更快的动画
        style.outerRadiusScale = 0.85;         // 分散布局使用更大的外圆        style.innerRadiusScale = 0.25;         // 更小的内圆，给菜单项更多空间
        style.menuItemBgScale = BUTTON_SIZE_SCALE * 0.9; // 稍小的菜单项，避免重叠
    } else {
        style.useScatteredLayout = false;
        style.enableRotationAnimation = true;  // 圆形布局启用旋转动画
        style.animationDuration = 400;         // 较慢的优雅动画
        style.outerRadiusScale = 0.80;         // 恢复原始外圆大小
        style.innerRadiusScale = 0.35;         // 恢复原始内圆大小        style.menuItemBgScale = BUTTON_SIZE_SCALE; // 恢复原始菜单项大小
    }
    
    // 应用新样式
    m_roundMenu->setMenuStyle(style);
    
    // 清除现有菜单项并重新添加，确保功能和图标正确
    m_roundMenu->clearMenuItems();
    setupMenuItems();
    
    // 强制重新绘制
    m_roundMenu->update();
    
    // 保存设置
    QSettings settings;
    settings.setValue("layout", static_cast<int>(layout));
}

void FloatingMenuWidget::loadSettings()
{
    QSettings settings;
    // 强制使用圆形布局
    SettingsDialog::LayoutType layout = SettingsDialog::CircularLayout;
    
    // 也可以从设置中读取，但优先使用圆形布局
    // int layoutValue = settings.value("layout", static_cast<int>(SettingsDialog::CircularLayout)).toInt();
    // SettingsDialog::LayoutType layout = static_cast<SettingsDialog::LayoutType>(layoutValue);
    
    // 应用圆形布局设置
    MenuStyleConfig style = m_roundMenu->getMenuStyle();
    
    style.useScatteredLayout = false;      // 强制设置为圆形布局
    style.enableRotationAnimation = true;
    style.animationDuration = 400;    style.outerRadiusScale = 0.80;
    style.innerRadiusScale = 0.35;
    style.menuItemBgScale = BUTTON_SIZE_SCALE;
    
    m_roundMenu->setMenuStyle(style);
    
    // 设置菜单项（只调用一次）
    setupMenuItems();
}

void FloatingMenuWidget::resetToCircularLayout()
{
    // 清除保存的设置
    QSettings settings;
    settings.remove("layout");
    
    // 强制应用圆形布局
    MenuStyleConfig style = m_roundMenu->getMenuStyle();
    style.useScatteredLayout = false;
    style.enableRotationAnimation = true;
    style.animationDuration = 400;
    style.outerRadiusScale = 0.80;
    style.innerRadiusScale = 0.35;
    style.menuItemBgScale = BUTTON_SIZE_SCALE;
    
    m_roundMenu->setMenuStyle(style);
      // 重新设置菜单项
    setupMenuItems();
    
    // qDebug() << "重置为圆形布局";
}
